package com.jetgege.resumepoint;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button bt_DownLoad1,bt_DownLoad2,bt_DownLoad3;
    private Button bt_Cancel1,bt_Cancel2,bt_Cancel3;
    private ProgressBar pb_Progress1,pb_Progress2,pb_Progress3;
    private String url1="https://codeload.github.com/ReactiveX/RxAndroid/zip/2.x";
    private String url2="https://codeload.github.com/ReactiveX/RxAndroid/zip/2.x";
    private String url3="https://codeload.github.com/ReactiveX/RxAndroid/zip/2.x";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupUI();
    }

    private void setupUI()
    {
        bt_DownLoad1=(Button)findViewById(R.id.main_btn_down1);
        bt_DownLoad2=(Button)findViewById(R.id.main_btn_down2);
        bt_DownLoad3=(Button)findViewById(R.id.main_btn_down3);
        bt_Cancel1=(Button)findViewById(R.id.main_btn_cancel1);
        bt_Cancel2=(Button)findViewById(R.id.main_btn_cancel2);
        bt_Cancel3=(Button)findViewById(R.id.main_btn_cancel3);
        pb_Progress1=(ProgressBar)findViewById(R.id.main_progress1);
        pb_Progress2=(ProgressBar)findViewById(R.id.main_progress2);
        pb_Progress3=(ProgressBar)findViewById(R.id.main_progress3);
        bt_DownLoad1.setOnClickListener(this);
        bt_DownLoad2.setOnClickListener(this);
        bt_DownLoad3.setOnClickListener(this);
        bt_Cancel1.setOnClickListener(this);
        bt_Cancel2.setOnClickListener(this);
        bt_Cancel3.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.main_btn_down1:
                downLoad(url1,pb_Progress1);
                break;
            case R.id.main_btn_down2:
                downLoad(url2,pb_Progress2);
                break;
            case R.id.main_btn_down3:
                downLoad(url3,pb_Progress3);
                break;
            case R.id.main_btn_cancel1:
                DownloadManager.getInstance().cancel(url1);
                break;
            case R.id.main_btn_cancel2:
                DownloadManager.getInstance().cancel(url2);
                break;
            case R.id.main_btn_cancel3:
                DownloadManager.getInstance().cancel(url3);
                break;
        }
    }
    private <T extends View> T bindView(@IdRes int id){
        View viewByid =findViewById(id);
        return (T)viewByid;
    }

    private void downLoad(String url,ProgressBar pb_Progress)
    {
        DownloadManager.getInstance().download(url,new DownLoadObserver(){
            @Override
            public void onNext(DownloadInfo value) {
                super.onNext(value);
                pb_Progress.setMax((int)value.getTotal());
                pb_Progress.setProgress((int)value.getProgress());
            }
            @Override
            public void onComplete() {
                super.onComplete();
                if (downloadInfo!=null){
                    Toast.makeText(MainActivity.this,downloadInfo.getFileName()+
                            "-DownloadComplete",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
